 
var g_arrPage = {
    action: "getSource",
    url: document.location.href,
    title: document.title,
    keywords: "",
    description: "",
    robots: "",
    host: "",
    h1: "",
    h2: "",
    b: "",
    i: "",
    alt: "",
    body: "",
    h1_len: 0,
    h2_len: 0,
    b_len: 0,
    i_len: 0,
    alt_len: 0,
    body_len: 0,
    link_canonical: "",
    link_internal: 0,
    link_external: 0,
    link_internal_nofollow: 0,
    link_external_nofollow: 0,
    link_map: {}
};

GetTags("h1", "", 512);
GetTags("h2", "", 512);
GetTags("alt", "", 512);
GetTags("body", "", 128);
GetTags("b", "strong", 512);
GetTags("i", "em", 512);

// 获取meta标签信息
for (var metas = document.getElementsByTagName("meta"), i = metas.length - 1; 0 <= i; i--) {
    var t_strName = metas[i].name.toLowerCase();
    if (t_strName === "keywords") {
        g_arrPage.keywords = metas[i].content;
    } else if (t_strName === "description") {
        g_arrPage.description = metas[i].content;
    } else if (t_strName === "robots") {
        g_arrPage.robots = metas[i].content;
    }
}

// 获取canonical链接
for (var t_arrCLink = document.getElementsByTagName("link"), i = t_arrCLink.length - 1; 0 <= i; i--) {
    if (t_arrCLink[i].rel.toLowerCase() === "canonical") {
        g_arrPage.link_canonical = t_arrCLink[i].href;
    }
}

// 处理body内容
var body_clone = document.body.cloneNode(true);
var trash = body_clone.querySelectorAll("script, iframe, style, textarea, input, select");
for (var j = trash.length - 1; 0 <= j; j--) {
    if (trash[j].parentNode) {
        trash[j].parentNode.removeChild(trash[j]);
    }
}

g_arrPage.content = body_clone.innerText;

// 获取主机名
var hostname = document.location.hostname;
if (hostname.substr(0, 4) === "www.") {
    hostname = hostname.substr(4);
}
g_arrPage.host = hostname;

// 处理链接
var links = body_clone.getElementsByTagName("A");
for (var i = 0, l = links.length; i < l; i++) {
    if (links[i].href && (links[i].protocol === "http:" || links[i].protocol === "https:")) {
        var href = links[i].href.replace(/#.*/, "");
        
        if (href in g_arrPage.link_map) {
            g_arrPage.link_map[href].text[g_arrPage.link_map[href].count] = GetLinkText(links[i]);
            g_arrPage.link_map[href].count += 1;
        } else {
            var t_strCurHost = links[i].hostname.replace(/^www\./, "").replace(/:\d+$/, "");
            g_arrPage.link_map[href] = {
                count: 1,
                href: href,
                hostname: t_strCurHost,
                nofollow: links[i].hasAttribute("rel") && links[i].getAttribute("rel").match(/nofollow/i) ? 1 : 0,
                text: {
                    "0": GetLinkText(links[i])
                },
                is_internal: hostname === t_strCurHost ? 1 : 0
            };
            
            if (t_strCurHost === hostname) {
                g_arrPage.link_internal++;
                if (g_arrPage.link_map[href].nofollow === 1) {
                    g_arrPage.link_internal_nofollow++;
                }
            } else {
                g_arrPage.link_external++;
                if (g_arrPage.link_map[href].nofollow === 1) {
                    g_arrPage.link_external_nofollow++;
                }
            }
        }
    }
}

// 清理内存
links = null;

// 使用chrome.runtime替代chrome.extension
try {
    chrome.runtime.sendMessage(g_arrPage);
} catch (error) {
    console.error("发送消息失败:", error);
}

function GetImgSrc(a, c) {
    var b = {
        type: "IMG",
        data: ""
    };
    
    if (a.nodeName === "IMG") {
        b.data = a.src;
        return b;
    } else if (a.firstChild != null) {
        b = GetImgSrc(a.firstChild, c + 1);
        if (b != null) return b;
    }
    
    if (c > 0 && a.nextSibling != null) {
        b = GetImgSrc(a.nextSibling, c + 1);
        if (b != null) return b;
    }
    
    return null;
}

function GetLinkText(a) {
    var c = {
        type: "NA",
        data: ""
    };
    
    var b = a.innerText.replace(/^\s+|\s+$/g, "");
    
    if (b === "") {
        c = GetImgSrc(a, 0);
        if (c == null) {
            if (a.innerHTML === undefined || a.innerHTML === "") {
                c = {
                    type: "NA",
                    data: ""
                };
            } else {
                c = {
                    type: "TXT",
                    data: a.innerHTML.replace(/</ig, "&lt;").replace(/>/ig, "&gt;")
                };
            }
        }
    } else {
        c.type = "TXT";
        c.data = b.replace(/</ig, "&lt;").replace(/>/ig, "&gt;");
    }
    
    return c;
}

function StripTags(a) {
    a = a.replace(/(<script([^>]+)>([^<]+)<\/script>)/ig, "");
    a = a.replace(/(<noscript([^>]+)>([^<]+)<\/noscript>)/ig, "");
    return a.replace(/(<([^>]+)>)/ig, "");
}

function StripSpaces(a) {
    return a.replace(/\s{2,}/g, " ");
}

function MakeTagsVale(a, c) {
    var b = StripSpaces(StripTags(c));
    if (b !== "") {
        if (g_arrPage[a] !== "") {
            g_arrPage[a] += " | ";
        }
        g_arrPage[a] += b;
        g_arrPage[a + "_len"] += b.length;
    }
}

function GetTags(a, c, b) {
    var d, e = document.getElementsByTagName(a === "alt" ? "img" : a);
    
    for (d = 0; d < e.length; d++) {
        MakeTagsVale(a, a === "alt" ? e[d].alt : e[d].innerHTML);
    }
    
    if (c !== "" && g_arrPage[a].length < b) {
        e = document.getElementsByTagName(c);
        for (d = 0; d < e.length; d++) {
            MakeTagsVale(a, e[d].innerHTML);
        }
    }
    
    if (g_arrPage[a].length > b) {
        g_arrPage[a] = g_arrPage[a].substr(0, b) + "...";
    }
}